package org.darcy.controller.admin;

import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.darcy.entity.BizArticle;
import org.darcy.framework.annotation.BussinessLog;
import org.darcy.framework.blog.business.enums.AdPositionEnum;
import org.darcy.framework.blog.business.enums.AdTypeEnum;
import org.darcy.framework.blog.core.BlogHunterConfigProvider;
import org.darcy.framework.blog.core.websocket.WebsocketServer;
import org.darcy.framework.blog.exception.BlogException;
import org.darcy.framework.util.ResultUtil;
import org.darcy.framework.util.SessionUtil;
import org.darcy.hunter.config.platform.Platform;
import org.darcy.hunter.enums.ExitWayEnum;
import org.darcy.service.BizArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 后台管理页面
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private BizArticleService articleService;

	@Autowired
	private WebsocketServer websocketServer;

	@Autowired
	private BlogHunterConfigProvider blogHunterConfigProvider;

	@RequiresAuthentication
	@BussinessLog("进入首页")
	@GetMapping()
	public ModelAndView home() {
		Long userId = SessionUtil.getCommonUserId();
		if (userId != null) {
			return ResultUtil.redirect("/admin/articles");
		}
		return ResultUtil.view("/admin/index");
	}

	@RequiresPermissions("users")
	@BussinessLog("进入用户列表页")
	@GetMapping("/users")
	public ModelAndView user() {
		return ResultUtil.view("/admin/user/user/list");
	}

	@RequiresPermissions("resources")
	@BussinessLog("进入资源列表页")
	@GetMapping("/resources")
	public ModelAndView resources() {
		return ResultUtil.view("/admin/user/resources/list");
	}

	@RequiresPermissions("roles")
	@BussinessLog("进入角色列表页")
	@GetMapping("/roles")
	public ModelAndView roles() {
		return ResultUtil.view("/admin/user/role/list");
	}

	@RequiresPermissions("articles")
	@BussinessLog("进入文章列表页")
	@GetMapping("/articles")
	public ModelAndView articles() {
		return ResultUtil.view("/admin/article/list");
	}

	@RequiresPermissions("article:publish")
	@BussinessLog(value = "进入发表文章页[{1}]")
	@GetMapping("/article/publish-{type}")
	public ModelAndView article_publish(@PathVariable("type") String type) {
		if (!Arrays.asList("we", "md", "tiny").contains(type)) {
			throw new BlogException("不支持的编辑器类型");
		}
		return ResultUtil.view("/admin/article/publish-" + type);
	}

	@RequiresPermissions("article:publish")
	@BussinessLog(value = "进入修改文章页[id={1}]")
	@GetMapping("/article/update/{id}")
	public ModelAndView article_update(@PathVariable("id") Long id, Model model) {
		model.addAttribute("id", id);
		BizArticle article = articleService.getById(id);

		if (!Arrays.asList("we", "md", "tiny").contains(article.getEditorType())) {
			throw new BlogException("文章异常，未知的编辑器类型");
		}
		return ResultUtil.view("/admin/article/publish-" + article.getEditorType());
	}

	@RequiresPermissions("types")
	@BussinessLog("进入分类列表页")
	@GetMapping("/article/types")
	public ModelAndView article_types() {
		return ResultUtil.view("/admin/article/types");
	}

	@RequiresPermissions("tags")
	@BussinessLog("进入标签列表页")
	@GetMapping("/article/tags")
	public ModelAndView article_tags() {
		return ResultUtil.view("/admin/article/tags");
	}

	@RequiresPermissions("links")
	@BussinessLog("进入链接页")
	@GetMapping("/links")
	public ModelAndView links() {
		return ResultUtil.view("/admin/site/link/list");
	}

	@RequiresPermissions("comments")
	@BussinessLog("进入评论页")
	@GetMapping("/comments")
	public ModelAndView comments() {
		return ResultUtil.view("/admin/site/comment/list");
	}

	@RequiresPermissions("notices")
	@BussinessLog("进入系统通知页")
	@GetMapping("/notices")
	public ModelAndView notices() {
		return ResultUtil.view("/admin/site/notice/list");
	}

	@RequiresPermissions("files")
	@BussinessLog("进入文件管理页面")
	@GetMapping("/files")
	public ModelAndView files(Model model) {
		return ResultUtil.view("/admin/site/file/list");
	}

	@RequiresPermissions("socials")
	@BussinessLog("进入社会化登录配置管理页面")
	@GetMapping("/socials")
	public ModelAndView socials(Model model) {
		return ResultUtil.view("/admin/site/social/list");
	}

	@RequiresPermissions("page")
	@BussinessLog("进入配置自定义页面")
	@GetMapping("/page")
	public ModelAndView page(Model model) {
		return ResultUtil.view("/admin/site/page/page");
	}

	@RequiresPermissions("bizAds")
	@BussinessLog("进入广告页面")
	@GetMapping("/bizAd")
	public ModelAndView bizAd(Model model) {
		model.addAttribute("positions", AdPositionEnum.toListMap());
		model.addAttribute("types", AdTypeEnum.toListMap());
		return ResultUtil.view("/admin/site/bizAd/bizAd");
	}

	@RequiresRoles("role:root")
	@BussinessLog("进入系统配置页")
	@GetMapping("/config")
	public ModelAndView config() {
		return ResultUtil.view("/admin/config");
	}

	@RequiresPermissions("templates")
	@BussinessLog("进入模板管理页")
	@GetMapping("/templates")
	public ModelAndView templates() {
		return ResultUtil.view("/admin/site/template/list");
	}

	@RequiresPermissions("logs")
	@BussinessLog("进入系统日志页")
	@GetMapping("/logs")
	public ModelAndView logs() {
		return ResultUtil.view("/admin/site/syslog/list");
	}

	@RequiresPermissions("icons")
	@BussinessLog(value = "进入icons页")
	@GetMapping("/icons")
	public ModelAndView icons(Model model) {
		return ResultUtil.view("/admin/other/icons");
	}

	@RequiresPermissions("shiro")
	@BussinessLog(value = "进入shiro示例页")
	@GetMapping("/shiro")
	public ModelAndView shiro(Model model) {
		return ResultUtil.view("/admin/other/shiro");
	}

	@RequiresUser
	@BussinessLog("进入编辑器测试用例页面")
	@GetMapping("/editor")
	public ModelAndView editor(Model model) {
		return ResultUtil.view("/admin/other/editor");
	}

	@RequiresPermissions("notice")
	@BussinessLog("进入通知管理页")
	@GetMapping("/notice")
	public ModelAndView notice(Model model) {
		model.addAttribute("online", websocketServer.getOnlineUserCount());
		return ResultUtil.view("/admin/laboratory/notification");
	}

	//@RequiresUser
	@BussinessLog("进入搬运工页面")
	@GetMapping("/remover")
	public ModelAndView remover(Model model) {
		model.addAttribute("exitWayList", ExitWayEnum.values());
		model.addAttribute("spiderConfig", blogHunterConfigProvider.getBlogHunterConfig());
		model.addAttribute("platforms", Platform.values());
		return ResultUtil.view("/admin/laboratory/remover");
	}

}
